package com.feihong.yw189.bluetooth.utils.djlm.DJLMReader3.reader;

import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;

import java.util.Arrays;

/**
 * Created by Sabirjan on 2017/4/8.
 */
public class ParseCardBaseInfo {

    private static final String TAG = "蓝牙";
    public byte[] m_data;
    public byte[] m_SW;
    public String m_strErrorInfo;
    public int m_nErrorCode;
    private final int SEEK_SIZE = 14;
    private static boolean loadLibSuccess = false;

    public ParseCardBaseInfo() {
    }

    private String GetNationDescribe(int nNationCode) {
        String[] strList = new String[]{"汉族", "蒙古族", "回族", "藏族", "维吾尔族", "苗族", "彝族", "壮族", "布依族", "朝鲜族",
                "满族", "侗族", "瑶族", "白族", "土家族", "哈尼族", "哈萨克族", "傣族", "黎族", "傈僳族",
                "佤族", "畲族", "高山族", "拉祜族", "水族", "东乡族", "纳西族", "景颇族", "柯尔克孜族", "土族",
                "达翰尔族", "仫佬族", "羌族", "布朗族", "撒拉族", "毛南族", "仡佬族", "锡伯族", "阿昌族", "普米族",
                "塔吉克族", "怒族", "乌孜别克族", "俄罗斯族", "鄂温克族", "德昂族", "保安族", "裕固族", "京族", "塔塔尔族",
                "独龙族", "鄂伦春族", "赫哲族", "门巴族", "珞巴族", "基诺族", "其它"};
        String strRet = "外国人入籍";
        if (nNationCode >= 1 && nNationCode <= 57)
            strRet = strList[nNationCode - 1];
        return strRet;
    }


    public String GetWLTData(byte[] data) {
        try {
            byte[] wltData = Arrays.copyOfRange(data, SEEK_SIZE + 256, SEEK_SIZE + 256 + 1024);
            return Base64.encodeToString(wltData, Base64.DEFAULT);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "";
    }

    private String GetName(byte[] data) {
        byte[] Name = new byte[30];
        try {
            System.arraycopy(data, SEEK_SIZE, Name, 0, 30);
            String strName = new String(Name, "UTF-16LE");
            Log.i(TAG, "GetName: " + strName);
            return strName;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }

    private String GetSex(byte[] data) {
        byte[] Sex = new byte[2];
        try {
            System.arraycopy(data, SEEK_SIZE + 30, Sex, 0, 2);
            String strSex = new String(Sex, "UTF-16LE");
            if (strSex.equals("1"))
                strSex = "男";
            else
                strSex = "女";
            return strSex;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }

    private String GetNation(byte[] data) {
        byte[] nationCode = new byte[4];
        try {
            System.arraycopy(data, SEEK_SIZE + 32, nationCode, 0, 4);
            String strNationCode = new String(nationCode, "UTF-16LE");
            Log.i(TAG, "GetNation: " + strNationCode);
            strNationCode = GetNationDescribe(Integer.parseInt(strNationCode));
            return strNationCode;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }

    private String GetBirth(byte[] data) {
        byte[] birth = new byte[16];
        try {
            System.arraycopy(data, SEEK_SIZE + 36, birth, 0, 16);
            String strBirth = new String(birth, "UTF-16LE");
            strBirth = strBirth.substring(0, 4) + "年" + strBirth.substring(4, 6) + "月" + strBirth.substring(6, 8) + "日";
            return strBirth;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }

    private String GetAddress(byte[] data) {
        byte[] Address = new byte[70];
        try {
            System.arraycopy(data, SEEK_SIZE + 52, Address, 0, 70);
            String strAddress = new String(Address, "UTF-16LE");
            return strAddress;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }

    private String GetIDCode(byte[] data) {
        byte[] ID = new byte[36];
        try {
            System.arraycopy(data, SEEK_SIZE + 122, ID, 0, 36);
            String strID = new String(ID, "UTF-16LE");
            return strID;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }

    private String GetRegorg(byte[] data) {
        byte[] Regorg = new byte[30];
        try {
            System.arraycopy(data, SEEK_SIZE + 158, Regorg, 0, 30);
            String strRegorg = new String(Regorg, "UTF-16LE");
            return strRegorg;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }

    private String GetStartDate(byte[] data) {
        byte[] startDate = new byte[16];
        try {
            System.arraycopy(data, SEEK_SIZE + 188, startDate, 0, 16);
            String strStartDate = new String(startDate, "UTF-16LE");
            strStartDate = strStartDate.substring(0, 4) + "年" + strStartDate.substring(4, 6) + "月" + strStartDate.substring(6, 8) + "日";
            return strStartDate;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }

    private String GetEndDate(byte[] data) {
        byte[] endDate = new byte[16];
        try {
            System.arraycopy(data, SEEK_SIZE + 204, endDate, 0, 16);
            String strEndDate = new String(endDate, "UTF-16LE");
            if (!TextUtils.isEmpty(strEndDate) && strEndDate.contains("长期")) return strEndDate;
            strEndDate = strEndDate.substring(0, 4) + "年" + strEndDate.substring(4, 6) + "月" + strEndDate.substring(6, 8) + "日";
            return strEndDate;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }



    public String GetJSON(byte[] data) {
        String strInfo;
        strInfo = "{\"Name\":\"" + GetName(data) + "\""
                + ",\"Sex\":\"" + GetSex(data) + "\""
                + ",\"Nation\":\"" + GetNation(data) + "\""
                + ",\"Born\":\"" + GetBirth(data) + "\""
                + ",\"Address\":\"" + GetAddress(data) + "\""
                + ",\"CardNo\":\"" + GetIDCode(data) + "\""
                + ",\"Police\":\"" + GetRegorg(data) + "\""
                + ",\"ActivityLFrom\":\"" + GetStartDate(data) + "\""
                + ",\"ActivityLTo\":\"" + GetEndDate(data) + "\"}";
        //log.info(strInfo);
        return strInfo;
    }



    public Bitmap parseBitmap(String strBmpURL, String macAddress, byte[] data) {
        try {
            // 图片解密，先用卡尔的本地解码，如果本地解码失败，再用服务器解码...
            Bitmap bitmap = null;
            if (strBmpURL != null) {
                try {
                    bitmap = BmpDecrypt.BmpDecryptRemote(strBmpURL, macAddress, "", GetWLTData(data));
                    if (bitmap != null) {
                        //远程解码成功
                        return bitmap;
                    } else {
                        Log.e(TAG, "run: 无法获取解码的图片");
                        //远程解码失败
                    }
                } catch (Exception ex) {
                    Log.e(TAG, "run: 解析图片失败！" + ex.getMessage());
                    ex.printStackTrace();

                }
            } else {
                Log.e(TAG, "run: 图片解析失败！服务器解码地址空");
            }
        } catch (Exception ex) {

            ex.printStackTrace();

        }
        return null;
    }

    public CardInfo GetInfo(byte[] data) {
        CardInfo cardInfo = new CardInfo();
        cardInfo.str_name = GetName(data).trim();
        cardInfo.str_Address = GetAddress(data).trim();
        cardInfo.str_Birth = GetBirth(data).trim();
        cardInfo.str_Nation = GetNation(data).trim();
        cardInfo.str_sex = GetSex(data).trim();
        cardInfo.str_IDCode = GetIDCode(data).trim();
        cardInfo.str_Regorg = GetRegorg(data).trim();
        cardInfo.str_startDate = GetStartDate(data).trim();
        cardInfo.str_endDate = GetEndDate(data).trim();
        return cardInfo;
    }

}
